$(function () {
    $("#jqGrid").jqGrid({
        url: '../zipmatch/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true,hidden:true },
			{ label: '文件MD5值', name: 'md5', index: 'md5', width: 80 }, 			
			{ label: '文件存储路径', name: 'path', index: 'path', width: 80 },
            { label: 'match类型', name: 'matchType', index: 'match_type', width: 80 },
            { label: '添加时间', name: 'addTime', index: 'add_time', width: 80 },
			{ label: '文件大小', name: 'size', index: 'size', width: 80 }			
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });


	new AjaxUpload('#upload', {
		action: '../sys/oss/upload',
		name: 'file',
		autoSubmit: true,
		responseType: "json",
		onSubmit: function (file, extension) {
			// if (!(extension && /^(jpg|jpeg|png|gif|bin|apk|zip|conf|patch)$/.test(extension.toLowerCase()))) {
			//     alert('只支持jpg、png、gif、bin格式的文件！');
			//     return false;
			// }
		},
		onComplete: function (file, r) {
			if (r.code == 0) {
				$("#downloadPath").val(r.url);
				$("#fileId").val(r.fileKey);
				$("#fileSize").val(r.fileSize);
				alert('文件更新成功！');
			} else {
				alert(r.msg);
			}
		}
	});
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		zipMatch: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.zipMatch = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {


            var matchTypeSelect = $("#matchTypeSelect").val();
            var downloadPath = $("#downloadPath").val();
            var fileSize = $("#fileSize").val();
            var md5 = $("#fileId").val();
            vm.zipMatch.md5 = md5;
            vm.zipMatch.path = downloadPath;
            vm.zipMatch.size = fileSize;
            vm.zipMatch.matchType = matchTypeSelect;
			var url = vm.zipMatch.id == null ? "../zipmatch/save" : "../zipmatch/update";
			$.ajax({
				type: "POST",
			    url: url,
			    data: JSON.stringify(vm.zipMatch),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: "../zipmatch/delete",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get("../zipmatch/info/"+id, function(r){
                vm.zipMatch = r.zipMatch;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		}
	}
});